<?php
Class NewsController extends Controller{

	public function show($id){
		$news = $this->loader->model('news');
		$news->SelectNewsFromSql($id);
		$hrefedit = Tools::get_href('/?q=news/edit/'.$id);
		$this->smarty->assign('news', $news);
		$this->smarty->assign('title', 'Kémia éve - '.strip_tags($news->GetHeader()));
		$this->smarty->assign('teacher', $_SESSION['is_teacher']);
		$this->smarty->assign('hrefedit', $hrefedit);
	}
	
	public function showall(){
		$news = $this->loader->model('news');
		$arch = $news->SelectAllNewsFromSql();
		$newscount = count($arch);
		$this->smarty->assign('arch', $arch);
		$this->smarty->assign('newscount', $newscount);
		$this->smarty->assign('title', 'Kémia éve - Minden Hír');
		$this->smarty->assign('teacher', $_SESSION['is_teacher']);
	}
	
	public function newnew(){
		$news = $this->loader->model('news');
		if(!empty($_POST)){
				$news -> setHeader(($_POST['header']));
				$news -> setDescription(stripslashes(($_POST['description'])));
				$news -> setBody(stripslashes(($_POST['body'])).'<br />');
				$news -> InsertNewsToSql();
				Routing::redirect('?q=news/showall');
		}
		$this->smarty->assign('title', 'Kémia éve - Új Hír');
		$this->smarty->assign('teacher', $_SESSION['is_teacher']);
	}
	
	public function edit($id){
		$news = $this->loader->model('news');
		$news->SelectNewsFromSql($id);
		if (!empty($_POST)){
			$news->setHeader($_POST['header']);
			$news->setDescription($_POST['description']);
			$news->setBody($_POST['body']);
			$news->setDate($_POST['date']);
			$news->UpdateNewsInSql();
			Routing::redirect('?q=news/show/'.$id);
		}
		$this->smarty->assign('news', $news);
		$this->smarty->assign('id', $id);
		$this->smarty->assign('title', 'Kémia éve - Hír Módosítása');
		$this->smarty->assign('teacher', $_SESSION['is_teacher']);
	}
	
	public function delete($id){
		$news = $this->loader->model('news');
		$news->DeleteNewsFromSql($id);
		Routing::redirect('?q=news/showall');
		$this->smarty->assign('news', $news);
		$this->smarty->assign('title', 'Kémia éve - Hír Törlése');
		$this->smarty->assign('teacher', $_SESSION['is_teacher']);
	}
	
}
